#include "filename.hpp"

Filename::Filename(std::string cpp_ext_in) //construct from std::string
{
	fn_cpp = trimString(cpp_ext_in);
	fn_original = wxString (fn_cpp.c_str(), wxConvUTF8);
	fn_core = trimName(fn_original);
}

Filename::Filename(wxString wx_ext_in)	//construct from wxWidgets-string
{
	fn_original = trimString(wx_ext_in);
	fn_cpp = std::string(fn_original.mb_str()); //wxString::ToStdString() not available in 2.8
	fn_core = trimName(fn_original);
}

wxString Filename::getComp() const	//get lowercase filename, complete w/o extension
{
	wxString CompString = fn_original;

	if (CompString.rfind(wxT(".")) != wxString::npos)
		CompString.resize(CompString.rfind('.'));	// crop off extension (see also next Filename::trimName() )

	return CompString.Lower();
}


wxString Filename::trimName(wxString wx_ext_in) const		//turn full into core filename
{
	if (wx_ext_in.rfind(wxT(".")) != wxString::npos)
		wx_ext_in.resize(wx_ext_in.rfind('.'));		// crop off extension (see also Filename::getComp() )

	if (wx_ext_in.rfind(wxT("_")) != wxString::npos)			//crop off possible EFF number, but only start loop if underscore found
	{
		for (int i = 0; i <= 9999; i++)			//loop over possible numbers, don't just crop everything after an underscore!
		{
			if (wx_ext_in.rfind(wxT("_") + paddedInt(i)) != wxString::npos)
			{
				wx_ext_in.resize(wx_ext_in.rfind(wxT("_") + paddedInt(i)));
				break;
			}
		}
	}

	//turn into lowercase
	wx_ext_in.MakeLower();

	//crop off possible specifiers
	if (wx_ext_in.size() >= 5 && wx_ext_in.rfind(wxT("-glow")) == wx_ext_in.size() - 5)
		wx_ext_in.resize(wx_ext_in.size() - 5);

	if (wx_ext_in.size() >= 7 && wx_ext_in.rfind(wxT("-normal")) == wx_ext_in.size() - 7)
		wx_ext_in.resize(wx_ext_in.size() - 7);

	if (wx_ext_in.size() >= 6 && wx_ext_in.rfind(wxT("-shine")) == wx_ext_in.size() - 6)
		wx_ext_in.resize(wx_ext_in.size() - 6);

	if (wx_ext_in.size() >= 6 && wx_ext_in.rfind(wxT("-trans")) == wx_ext_in.size() - 6)  //crop off this as last, maps like xxx-trans-glow are allowed
		wx_ext_in.resize(wx_ext_in.size() - 6);

	return wx_ext_in;
}

wxString Filename::paddedInt (int in) const		//turn integer into 4-digit zero-padded string, e.g. 0034
{
	//Largely based on http://www.cplusplus.com/forum/general/15952/
	std::stringstream ss;
	std::string daString;
	int strlen = 0;

	//convert number to string
	ss << in;
	ss >> daString;

	//append zeroes
	strlen = daString.length();
	for (int i = 0; i < 4 - strlen; i++)
	{
		daString = "0" + daString;
	}
	wxString daWxString(daString.c_str(), wxConvUTF8);
	return daWxString;
}

wxString Filename::trimString (wxString TrimThis) //trim leading, trailing spaces, comments
{
	if (TrimThis.length() == 0)
		return TrimThis;
	else
	{
		//Find comments
		size_t comment = TrimThis.find_first_of(';');
		if (comment != wxString::npos)
			TrimThis.resize(comment);
	}
	//String trimmed, check length again
	if (TrimThis.length() == 0)
		return TrimThis;
	else
	{
		//Find leading, trailing spaces & tabs & stuff
		size_t startshere = TrimThis.find_first_not_of(wxT(" \t\r"));
		size_t length = TrimThis.find_last_not_of(wxT(" \t\r")) + 1 - startshere;

		//This will trigger on e.g. space-only strings
		if (startshere == wxString::npos || length == wxString::npos)
			return wxString ();
		else
			return TrimThis.substr(startshere, length);
	}
}

std::string Filename::trimString (std::string TrimThis) //trim leading, trailing spaces, comments
{
	if (TrimThis.length() == 0)
		return TrimThis;
	else
	{
		//Find comments
		size_t comment = TrimThis.find_first_of(';');
		if (comment != wxString::npos)
			TrimThis.resize(comment);
	}
	//String trimmed, check length again
	if (TrimThis.length() == 0)
		return TrimThis;
	else
	{
		//Find leading, trailing spaces & tabs & stuff
		size_t startshere = TrimThis.find_first_not_of(" \t\r");
		size_t length = TrimThis.find_last_not_of(" \t\r") + 1 - startshere;

		//This will trigger on e.g. space-only strings
		if (startshere == wxString::npos || length == wxString::npos)
			return std::string();
		else
			return TrimThis.substr(startshere, length);
	}
}
